from service import Service
from biz_service import *
from local_region import LocalRegion

class Biz(Service):
    # year      => int
    # week      => int
    # source_11 => source 11 file path
    # source_13 => source 13 file path
    # result    => result_directory (must exist)
    @staticmethod
    def create(year, week, src_11, src_13, result):
        biz_args = Biz.__parse_args(year, week, src_11, src_13, result)
        #import pprint
        #pprint.pprint(biz_args)
        return Biz(biz_args)
    
    def run(self):
        year = self.__args.get('year') 
        if year is None or Biz.__is_year_invalid(year):
            return False
        week = self.__args.get("week")
        if week is None or Biz.__is_week_invalid(week):
            return False

        #1. LocalRegion(year, week, self.__args["local"]).run()
        local = self.__args.get("local")
        if local is None :
            return False
        if not LocalRegion(year, week, local).run():
            return False

        #2. TradeLine(year, week, self.__args["trade"]).run()
        trade = self.__args.get("trade")
        if trade is None:
            return False
        if not TradeLine(year, week, trade).run():
            return False

        #3. Sales Check 
        sales = self.__args.get("sales")
        if sales is None:
            return False

        #4. SalesRegion(year, week, self.__args["sales"]["region"]).run()
        sales_region = sales.get("region")
        if sales_region is None or (not SalesRegion(year, week, sales_region).run()):
            return False

        #5. SalesCountry(year, week, self.__args["sales"]["country"]["ib"]).run()
        sales_country = sales.get("country")
        if sales_country is None or (not SalesCountry(year, week, sales_country).run()):
            return False
        return True



    @staticmethod
    def __parse_args(year, week, source_11_name, source_13_name, result_dir):
        date_str =  "%s%2d" % (str(year), week)
        return {
            'year' : year,
            'week' : week,
            'local' : {
                'src' : source_11_name, 
                'result' : f'{result_dir}/result_local_region_week_{date_str}.csv',
            },
            'trade' : {
                'src' : source_11_name,
                'result' : f'{result_dir}/result_trade_line_week_{date_str}.csv',
            },
            'sales' : {
                'region' : {
                    'ib': {
                        'src' : source_13_name, 
                        'result' : f'{result_dir}/result_sales_region_week_ib_{date_str}.csv',
                    },
                    'ob': {
                        'src' : source_13_name, 
                        'result' : f'{result_dir}/result_sales_region_week_ob_{date_str}.csv',
                    },
                },
                'country' : {
                    'ib' : {
                        'src' : source_13_name, 
                        'result' : f'{result_dir}/result_sales_country_week_ib_{date_str}.csv',
                    }, 
                    'ob' : {
                        'src' : source_13_name, 
                        'result' : f'{result_dir}/result_sales_country_week_ob_{date_str}.csv',
                    },
                }
            }
        }
    def __init__(self, args):
        self.__args =  args

    @staticmethod
    def __is_year_invalid( value):
        return value < 1000 or value > 3000
    @staticmethod
    def __is_week_invalid(value):
        return value <= 0 or value >= 54



if __name__ == "__main__":
    year = 2024
    week = 10
    source_11_name = "./source/2024 wk10 1711.csv"
    source_13_name = "./source/2024 wk10 1713.csv"
    result_dir = "./result" 
    Biz.create(year, week, source_11_name, source_13_name, result_dir).run()
